package com.microcm.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import com.microcmframework.core.context.AppContext;

public class SharePropUtils
{
	private static SharePropUtils propUtil;

	private boolean loaded;
	private String webInf;
	private String filePath;
	private Properties prop = new Properties();

	private SharePropUtils()
	{
		webInf = AppContext.getInstance().getServletContext().getRealPath("/WEB-INF/");
	}

	public static SharePropUtils getInstance()
	{
		if (propUtil == null)
		{
			propUtil = new SharePropUtils();
		}
		return propUtil;
	}

	public boolean isLoaded()
	{
		return loaded;
	}

	/**
	 * 加载系统properties配置
	 * 
	 */
	public void loadPropConfig(String filePath)
	{
		this.filePath = filePath;
		File file = new File(webInf + filePath);
		try
		{
			if (!file.exists())
			{
				file.createNewFile();
			}
			prop.load(new FileInputStream(file));
			this.loaded = true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public String getValue(String key)
	{
		return this.prop.getProperty(key);
	}

	public void setValue(String key, String value)
	{
		this.prop.setProperty(key, value);
	}

	public void store()
	{
		try
		{
			prop.store(new FileOutputStream(webInf + this.filePath), null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
